package com.jone.bang.service;

import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

import com.jone.bang.model.BChannel;
import com.jone.bang.model.WordItem;
import com.jone.bang.util.Constant;
import com.jone.bang.util.DateUtil;
import com.jone.bang.util.RssLoadUtil;
import com.jone.bang.util.XmlParseUtil;




public class DownloadService {
	
	
	private Context context;
	
	private String content;
	
	private String theKey;
	
	
	public DownloadService(Context context){
		this.context = context;
	}

	public List<WordItem> download(String key,String url){
		this.theKey = key;
		//判断网络是否可连接
		ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE ); 
	    NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); 
	    if(activeNetInfo!=null && activeNetInfo.getState() == NetworkInfo.State.CONNECTED){
	    	
	    	long before = System.currentTimeMillis();
	    	//加载并组织xml
	    	content = RssLoadUtil.loadUrl(url);
//	    	Log.d("Time","load data:"+(System.currentTimeMillis()-before));
//	    	before = System.currentTimeMillis();
//	    	
//	    	//解析xml
//	    	content = XmlParseUtil.dom4jParse(xml);
//	    	Log.d("Time","Xml parse:"+(System.currentTimeMillis()-before));
//	    	before = System.currentTimeMillis();
	    	
	    	
	    	List<WordItem> items = FileManager.stringToList(content);
	    	
	    	Log.d("Time","convert to list:"+(System.currentTimeMillis()-before));
	    		    	
	    	Cache.getItemMap().put(key, items);
	    	
	    	new Thread(){	    		
	    		public void run(){
	    			FileManager.writeToFile(content, theKey+".txt");
	    		}	    		
	    	}.start();
	    	
	    	return items;
	    }
	    return null;		
	}
	
	public List<WordItem> getList(String key){
		
		String selection = BChannel.KEY + " = '" + key + "'";
		Cursor cursor = context.getContentResolver().query(BChannel.CHANNEL, null, selection, null, null);
		if(cursor != null && cursor.moveToFirst()){
			String updateTime = cursor.getString(cursor.getColumnIndex(BChannel.UPDATE_TIME));
			String url = cursor.getString(cursor.getColumnIndex(BChannel.RSS_URL));
			int id = cursor.getInt(cursor.getColumnIndex(BChannel._ID));
			cursor.close();
			
			long spanHour = 4;
			if(id == 1){
				//id为1-今日热榜  超过4小时就更新
				spanHour = Constant.MIN_UPDATE_SPAN_HOUR;
			}else if(id == 2){
				//id为2-一周热点   超过24小时就更新
				spanHour = Constant.MIDDLE_UPDATE_SPAN_HOUR;
			}else{
				//否则48小时才更新
				spanHour = Constant.MAX_UPDATE_SPAN_HOUR;
			}
			
			//是否大于更新间隔时间
			if(DateUtil.spanHour(updateTime)>=spanHour || "".equals(updateTime)){
				//重加载
				List<WordItem> items = download(key,url);
				if(items!= null && !items.isEmpty()){
					//加载成功后更新时间
					ContentValues values = new ContentValues();
					values.put(BChannel.UPDATE_TIME, DateUtil.getCurrentDate("yyyyMMddHHmmss"));
					context.getContentResolver().update(BChannel.CHANNEL, values, selection, null);
					return items;
				}				
			}else{
				List<WordItem> items = Cache.getItemMap().get(key);
				if(items != null && !items.isEmpty()){
					return items;
				}else{
					return FileManager.readFileToList(key+".txt");
				}
			}
		}
		return null;			
	}
	
	public String getNotificationString(String key){
		List<WordItem> list = getList(key);
		StringBuffer sb = new StringBuffer();
		if(list != null && !list.isEmpty()){
			for(int i=0;i<4;i++){
				sb.append(list.get(i).getName()).append("/ ");
			}
			sb.append("...");
		}
		return sb.toString();
		
	}
	
}